////////////////////////////////////////////////////////////////////////////////
//
//	This file is part of Woozox, see <http://code.google.com/p/woozox/>.
//
//	Copyright 2008-2009 Florian David
//
//	Woozoo is free software: you can redistribute it and/or modify
//	it under the terms of the GNU Lesser General Public License as 
//	published by the Free Software Foundation, either version 3 of 
//	the License, or (at your option) any later version.
//
//	Woozoo is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU Lesser General Public 
//	License along with Woozoo. If not, see <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////
package org.woozoo.xhtml.core.manager.element
{
	import org.woozoo.xhtml.core.base.*;
	import org.woozoo.xhtml.core.util.UIDUtil;
	
	/**
	 * @private
	 * */
	public class ElementManager
	{
		private static var __instance : ElementManager;
		protected static var _instanciable : Boolean = false;

		private var __elements : Object = {};
		
		public function ElementManager()
		{
			if (!_instanciable) {
				throw new Error('ElementManager is a singleton class, call CacheManager.getInstance() instead');
			}
		}
		
		public static function getInstance() : ElementManager {
			if (!__instance) {
				_instanciable = true;
				__instance = new ElementManager();
				_instanciable = false;
			}
			return __instance;
		}	
		
		public function addClass(tagName : String, classRef : Class) : void {
			ElementFactory.addClass(tagName, classRef);
		}
//		public function addRegExp(regExp : String, classRef : Class) : void {
//			ElementFactory.addRegExp(regExp, classRef);
//		}
		
		public function create(document : Document, source : *, classRef : Class = null) : Element {
			if (!source) {
				return ElementFactory.factory(document, source, classRef);
			}
			if (source is XML) {  
				var sourceXML : XML = source as XML;
				var sourceXMLUID : String = sourceXML.@wzx_internal_uid;
				if (!sourceXMLUID) {
					sourceXMLUID = sourceXML.@wzx_internal_uid = UIDUtil.createUID();
				} 
				var element : Element = __elements[sourceXMLUID];
				if (!element) {
					return  __elements[sourceXMLUID] = ElementFactory.factory(document, source, classRef);
				}
				return element;;
			}
			return ElementFactory.factory(document, source, classRef);
		}
	}
}